<?php 
require_once("../includes/functions.php");
require_once("../includes/session.php"); 
connect_db();
?>
<?php
$inputname = $_POST['userid'];
$inputpass = $_POST['userpass'];

$valid = authenticate_user($inputname, $inputpass);

if($valid == true){
  $usertype = get_user_type($inputname);
}

if(!isset($_SESSION['logged_in'])){ //If not logged in yet, log in
  if($usertype  == "Member"){
    session_start(); //Use existing sesion if found. If not, create a new cookie for session
    $_SESSION['logged_in'] = 1;
    $_SESSION['log_in_type'] = "member";
    $_SESSION['user_id'] = $inputname;
    redirect_to("../member/member.php");
  }
  if($usertype  == "Teller"){
    session_start(); //Use existing sesion if found. If not, create a new cookie for session
    $_SESSION['logged_in'] = 1;
    $_SESSION['log_in_type'] = "teller";
    $_SESSION['user_id'] = $inputname;
    redirect_to("../teller/teller.php");
  }
  if($usertype == "Admin"){
    session_start(); //Use existing sesion if found. If not, create a new cookie for session
    $_SESSION['logged_in'] = 1;
    $_SESSION['log_in_type'] = "admin";
    $_SESSION['user_id'] = $inputname;
    redirect_to("../admin/administrator_home.php");
  }
  else{
    redirect_to("../index.php?credentials=0");
  }
}
else{ //If logged in, then redirect
  redirect_to_base("member");
}
?>
